package cn.zjj.basic.controller;

import cn.zjj.basic.dto.LoginDto;
import cn.zjj.basic.dto.LogininfoDto;
import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.service.ILoginService;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.user.dto.WeChatBinderDto;
import cn.zjj.user.dto.WeChatLoginDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService iLoginService;

    @PostMapping("/wechatbinder")
    public JsonResult wechatBinder(@RequestBody WeChatBinderDto weChatBinderDto){
        try {
            LoginDto loginDto = iLoginService.wechatBinder(weChatBinderDto);
            return JsonResult.me().setResultObj(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().runtimeException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙,稍后重试!");
        }
    }


    @GetMapping("/wechat/{code}")
    public JsonResult wechatLogin(@PathVariable("code") String code){
        try {
            return iLoginService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().runtimeException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙,稍后重试!");
        }
    }




    @PostMapping("/account")
    public JsonResult account(@RequestBody LogininfoDto logininfoDto){
        try {
            LoginDto account = iLoginService.account(logininfoDto);
            return JsonResult.me().setResultObj(account);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().runtimeException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙,稍后重试!");
        }
    }
}
